# Copyright 2008 PNR Studios
# Last Edited 7/25/2008 Matt Wanninger
# projectile.rb

class Projectile
  attr_reader :x, :y, :player_projectile, :damage, :angle, :xspeed, :yspeed
  def initialize(animation, x, y, strength, xspeed, yspeed, angle, player_projectile, color)
   #animation = the picture file
   #x = starting x coordinate
   #y = y coordinate
   #strength = damage a projectile inflicts
   #speed = speed that the projectile travels
   #if player_projectile is true, then the projectile was fired by the player; this way, a player's projectile can't hurt himself
   #(this is needed as to make the animation look right, the projectile starts on top of the player's image, though on a transparent section)
    @image = animation
    @x = x
    @y = y
    @damage = strength
    @xspeed = xspeed
    @yspeed = yspeed
    @player_projectile = player_projectile
    @length_y = @image.height
    @width_x = @image.width
    @angle = angle
    @color = color
  end
  
  def draw
    @image.draw_rot(@x, @y , ZOrder::Projectile, @angle, 0.5, 0.5, 1, 1, @color)
  end
  
  #detects collision between a player's projectile and enemy/other obstacles
  #NOTE: UPDATE COLLISION METHOD SO ENEMIES ARE NOT LOOPED THROUGH TWICE!
def collision(enemies, player, hits, hit_anim, explode, particles, hit_sound)
    coll = false
    enemies.reject! do |enemy|
    if Gosu::distance(@x, @y, enemy.x, enemy.y) < enemy.radius + 2 and @player_projectile then
      coll = true
      hits.push(DrawHit.new(hit_anim, @x, @y, enemy))
      #5.times {particles.push( Particle.new(@x, enemy.y+enemy.radius, 'media/explosion.png', 0, 0, rand(360), 0, 0.0, 0.015, 8))}
      SDL::Mixer.setVolume(-1, 10)
      SDL::Mixer.setVolume(0, 100)       
      SDL::Mixer.playChannel(-1, hit_sound, 0)
      enemy.decrease_hp(@damage)
      if enemy.hp <= 0
        SDL::Mixer.setVolume(10, 20)     
        SDL::Mixer.playChannel(10, explode, 0)
        player.increase_score(enemy.score)
         10.times {particles.push( Particle.new(ZOrder::Explosion,enemy.x - rand(9) + 5 ,enemy.y - rand(9) + 5, 'media/explosion.png', rand(0) -0.5, rand(0) - 0.5, rand(360), 0,  1.5, 0.05, 7))}
        true
      else
        false
      end
    else
      false
    end
  end
return coll
end
  
  #right now this merely updates the y position of the projectile, but this will eventually
  #update x and y when projectiles are fired at angles
  def update
    @y += @yspeed
    @x += @xspeed
    @y < 0 or @y > 480 or @x < 120 or @x > 520
  end
end
